package ruifeng.zcj.dig.base;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import ruifeng.zcj.mybase.base.App;
import ruifeng.zcj.mybase.base.BaseFragment;

/**
 * Created by Administrator on 2016/9/3 0003.
 */
public abstract class BaseFragmentViewPageAdapter extends FragmentPagerAdapter {
    private Map<String, BaseFragment> fragments;
    private ArrayList<String> items;
    public BaseFragmentViewPageAdapter(FragmentManager fm) {
        super(fm);
        fragments =  new HashMap<>();
        items = new ArrayList<>();// 用来存放title
    }
    public void addTab(String title, String tag, Class<?> clss, Bundle args){
        items.add(title);
        fragments.put(title,(BaseFragment) Fragment.instantiate(App.getAppContext(), clss.getName(), args));
    }

    @Override
    public Fragment getItem(int position) {
        BaseFragment fragment = fragments.get(items.get(position));
        if (fragment == null) {
            fragment = newFragment(items.get(position));
            fragments.put(items.get(position), fragment);
        }
        return fragment;
    }

    @Override
    public int getCount() {
        return items.size();
    }


    public abstract BaseFragment newFragment(String item);
}
